package com.xialinrui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xialinrui.entity.Income;
import com.xialinrui.entity.User;
import com.xialinrui.mapper.IncomeMapper;
import com.xialinrui.service.IncomeService;
import com.xialinrui.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xialinrui
 */
@Service
public class IncomeServiceImpl extends ServiceImpl<IncomeMapper, Income> implements IncomeService {
    @Resource
    private IncomeMapper incomeMapper;

    @Resource
    private UserService userService;

    @Override
    public void deleteByIds(List<Long> ids) {
        for (Long id : ids) {
            //查询出expenditure
            QueryWrapper<Income> incomeQueryWrapper = new QueryWrapper<>();
            incomeQueryWrapper.eq("id", id);
            Income income = this.getOne(incomeQueryWrapper);
            //查询出user
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("user_id", 1);
            User user = userService.getOne(userQueryWrapper);
            //加钱
            user.setWholeMoney(user.getWholeMoney() - income.getMoney());
            //删除
            incomeMapper.deleteById(id);
        }

    }

    @Override
    public void addIncome(Income income) {
        //收入数据添加到数据库
        this.save(income);
        //用户总钱数增加
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_id", 1);
        User user = userService.getOne(userQueryWrapper);
        user.setWholeMoney(user.getWholeMoney() + income.getMoney());
        userService.updateById(user);
    }
}
